// commons/diy/new-coupon/index.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    show_new_coupon: 1
  },

  ready: function () {
    this.getNewCoupon()
  },
  /**
   * 组件的方法列表
   */
  methods: {
    // 获取DIY模块
    getNewCoupon: function () {
      var that = this;
      getApp().request({
        url: getApp().api.index.get_new_coupon,
        success: function(t) {
          that.setData({
            new_coupon_list: t.data.new_coupon_list,
          })
        }
      })
    },
    closeNewCoupon: function() {
        this.setData({
          show_new_coupon: 0
        })
    },
    receive_new: function (e) {
      console.log(e);
      var i = this,
        o = e.index ? e.index : e.currentTarget.dataset.index;
      var k = e.index ? e.key : e.currentTarget.dataset.key;
      wx.showLoading({
        title: "领取中",
        mask: !0
      }), i.hideGetCoupon || (i.hideGetCoupon = function (t) {
        var a = t.currentTarget.dataset.url || !1;
        i.setData({
          get_coupon_list: null
        }), wx.navigateTo({
          url: a || "/pages/list/list"
        });
      }), getApp().request({
        url: getApp().api.coupon.receive,
        data: {
          id: o
        },
        success: function (t) {
          if(e.type == 'all'){
            i.data.new_coupon_list.list[k].is_receive = 1, 0 == t.code ? i.setData({
              new_coupon_list: i.data.new_coupon_list
            }) : (i.setData({
              new_coupon_list: i.data.new_coupon_list
            }));
          }else{
            wx.hideLoading(), i.data.new_coupon_list.list[k].is_receive = 1, 0 == t.code ? i.setData({
              new_coupon_list: i.data.new_coupon_list
            }) : (wx.showToast({
              title: t.msg,
              duration: 2e3
            }), i.setData({
              new_coupon_list: i.data.new_coupon_list
            }));
          }
          
        }
      });
    },
    receive_arr:function (){
      var that = this;
      var new_coupon_list = that.data.new_coupon_list.list;
      for(var i = 0 ; i< new_coupon_list.length ; i++){
        var data = {index:new_coupon_list[i].id,key:i,type:'all'}
        that.receive_new(data);
      }
      wx.hideLoading(), wx.showToast({
        title: '领取完毕',
        duration: 2e3
      })
    },
  }
})
